package com.molichuxing.gateway.bff.car.services.impl;

import com.molichuxing.gateway.bff.car.convertor.Convert;
import com.molichuxing.gateway.bff.car.entity.request.modify.WarnConfigModifyVo;
import com.molichuxing.gateway.bff.car.entity.response.WarnConfigVo;
import com.molichuxing.gateway.bff.car.services.WarnConfigBffService;
import com.molichuxing.services.business.dto.response.CarWarnConfigDto;
import com.molichuxing.services.infrastructure.dto.request.modify.WarnConfigModifyDto;
import com.molichuxing.services.infrastructure.dto.response.WarnConfigDto;
import com.molichuxing.services.infrastructure.service.IovCarService;
import com.molichuxing.services.infrastructure.service.WarnConfigService;
import com.molichuxing.services.property.WarnConfigTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 车辆检验bff
 *
 * @author zhaopenghui
 */
@Service("warnConfigBffService")
public class WarnConfigBffServiceImpl implements WarnConfigBffService {

    private static final Logger logger = LoggerFactory.getLogger(WarnConfigBffServiceImpl.class);

    @Resource
    private WarnConfigService warnConfigService;
    @Resource
    private IovCarService iovCarService;


    /**
     * 获取配置列表
     *
     * @return
     */
    @Override
    public List<WarnConfigVo> getWarnConfigList() {
        CarWarnConfigDto carOfflineConfig = iovCarService.getCarOfflineConfig();
        List<WarnConfigDto> list = warnConfigService.getList();
        return Convert.toWarnConfigVoList(list, carOfflineConfig);
    }

    /**
     * 编辑配置
     *
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyWarnConfig(List<WarnConfigModifyVo> list) throws Exception {
        if (null == list || list.size() < 1) {
            return true;
        }

        for (WarnConfigModifyVo warnConfigModifyVo : list) {
            Integer type = warnConfigModifyVo.getType();
            if (type != null && WarnConfigTypeEnum.CAR_OFFLINE.getValue() == type) {
                Integer warnConfigId = warnConfigModifyVo.getWarnConfigId();
                String value = warnConfigModifyVo.getValue();
                iovCarService.modifyCarOfflineConfig(Long.valueOf(warnConfigId),Double.valueOf(value));
            }
        }

        List<WarnConfigModifyDto> warnConfigModifyDtoList = Convert.toWarnConfigModifyDtoList(list);
        return warnConfigService.modifyWarnConfig(warnConfigModifyDtoList);
    }

    /**
     * 编辑配置
     *
     * @param warnConfigModifyVo
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyWarnConfig(WarnConfigModifyVo warnConfigModifyVo) throws Exception {
        WarnConfigModifyDto warnConfigModifyDto = Convert.toWarnConfigModifyDto(warnConfigModifyVo);
        return warnConfigService.modifyWarnConfig(warnConfigModifyDto);
    }

}
